import Vue from 'vue'
import VueRouter from 'vue-router'
// 欢迎界面
import Welcome from "@/components/Welcome"
// 404
import NotFound from "@/components/NotFound"
import Goods from './modules/goods'
import Main from './modules/main'
import Merchant from './modules/merchant'
import Order from './modules/order'
import Setting from './modules/setting'
import User from './modules/user'
import Role from './modules/role'
Vue.use(VueRouter)
const router = new VueRouter({
    routes: [
        // 重定向
        { path: '/', redirect: '/login' },
        { path: '/login', component: () => import('@/views/Login') },
        {
            path: '/home',redirect:'/welcome', component: () => import('../views/Home.vue'),  children: [
                {
                    path: "/welcome",
                    component: Welcome
                },
                Goods,
                Main,
                Merchant,
                Order,
                Setting,
                User,
                Role
            ]
        },
        // 404页面
        { path: "*", component: NotFound }
    ]
})
// 白名单
const whiteList = ["/404", "/login"]
// 全局前置路由守卫
router.beforeEach((to, from, next) => {
    // if (to.path === '/login') return next()
    // // 获取token
    const tokenStr = window.sessionStorage.getItem("token")
    // if (!tokenStr) return next("/login")
    // next()
    // 如果有token
    if (tokenStr) {
        // 如果是去登录页
        if (to.path === '/login') {
            // 回到首页 因为我有token你不能随便去登录页 只有退出登录才可以
            next('/')
        } else {
            // 不是登录页随便跳
            next()
        }
    } else {
        // 如果没有token
        if (whiteList.includes(to.path)) {
            next()
        } else {
            next("/login")
        }
    }
})
export default router